package com.example.wishstar.repository;

import android.content.res.Resources;
import android.os.AsyncTask;
import android.util.Log;

import androidx.lifecycle.LiveData;

import com.example.wishstar.MainActivity;
import com.example.wishstar.MyApplication;
import com.example.wishstar.R;
import com.example.wishstar.api.Api;
import com.example.wishstar.db.UserDao;
import com.example.wishstar.model.Memory;
import com.example.wishstar.model.Users;
import com.example.wishstar.utils.ApiResponse;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MemoryRepository {
    private String TAG = "MemoryRepository";
    private UserDao userDao;
    private Api api;

    public MemoryRepository(UserDao userDao, Api api) {
        this.userDao = userDao;
        this.api = api;
    }

    public LiveData<List<Memory>> getMemorys(int userId) {
        refresh(userId);
        return userDao.getMemoryByuserId(userId);
    }

    public void deleteMemoryByMemoryId(Integer memoryId) {
        api.deleteMemory(memoryId).enqueue(new Callback<ApiResponse<String>>() {
            @Override
            public void onResponse(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                Log.e(TAG, "deleteMemory()->onResponse()");
                if(response.body() != null && response.body().code == 200) {
                    deleteMemory(memoryId);
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<String>> call, Throwable t) {
                Log.e(TAG, "deleteMemory()->onFailure()");
            }
        });
    }

    public void refresh(Integer userId) {
        api.getMemorys(userId).enqueue(new Callback<ApiResponse<List<Memory>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<Memory>>> call, Response<ApiResponse<List<Memory>>> response) {
                if(response.body() != null && response.body().code == 200) {
                    Log.e(TAG, "refresh->response" + response.body());
                    deleteMemoryByUserId(userId);
                    insertMemory(userId, response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<Memory>>> call, Throwable t) {
                Log.e(TAG, "refresh()->onFailure()");
            }
        });
    }

    public void insertMemory(int userId, List<Memory> memories) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i < memories.size(); ++i) {
                    Memory memory = memories.get(i);
                    memory.userId = userId;
                    if (memory.type == 2) {
                        memory.memoryTitle = memory.memoryTitle + " 还有";
                    }
                    else {
                        memory.memoryTitle = memory.memoryTitle + " 已经";
                    }

                    memory.dayNumString = "" + memory.dayNum;
                    userDao.insertMemory(memory);
                    Log.e(TAG, "insertMemory()->" + memory.toString());
                }
            }
        });
    }

    public void deleteMemory(int memoryId) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                userDao.deleteMemoryByMemoryId(memoryId);
            }
        });
    }

    private void deleteMemoryByUserId(int userId) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                userDao.deleteMemoryByUserId(userId);
            }
        });
    }
}
